using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;

namespace PS.WebParts.Rollup
{
    public abstract class ADataSourceView : DataSourceView
    {
        protected StringBuilder _processingMessages;
        Object cachedReturnType;
        DataSourceCollection cachedDataSources;

        public void ForceRollupRerun()
        {
            cachedReturnType = null;
        }

        internal Object CachedReturnType
        {
            get { return cachedReturnType; }
        }

        internal DataSourceCollection CachedDataSources
        {
            get { return cachedDataSources; }
        }

        protected void SetReturnType(Object data)
        {
            cachedReturnType = data;
        }

        protected void SetCachedDataSources(DataSourceCollection data)
        {
            cachedDataSources = data;
        }

        public string ProcessingMessages
        {
            get { return _processingMessages != null ? _processingMessages.ToString() : null; }
        }

        protected void LogError(string message)
        {
            LogError(message, null);
        }

        protected void LogError(string message, Exception exception)
        {
            if (_processingMessages == null)
                _processingMessages = new StringBuilder();

            _processingMessages.AppendFormat("Exception: {0}\n{1}\n\n", message,
                                            exception != null ? exception.ToString() : null);
        }

        internal abstract IEnumerable DoSelect(DataSourceSelectArguments arguments);
        protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
        {
            return DoSelect(arguments);
        }

        HttpContext context;

        protected HttpContext Context
        {
            get { return context; }
        }

        protected ADataSourceView(HttpContext context, IDataSource owner, string viewName)
            : base(owner, viewName)
        {
            this.context = context;
        }

        /// <summary>
        /// Returns an empty datatable based on columns defined in the ListFields and IncludeListData attributes
        /// </summary>
        /// <returns></returns>
        public abstract DataTable GetSchema();
        
        private string topLevelWeb;
        public string TopLevelSite
        {
            get
            {
                return topLevelWeb;
            }
            set
            {

                if (value == null)
                    throw new ArgumentNullException("value");
                // VAL> Inserted 1/20/07 to troubleshoot issue with spaces as a part of url
                string val;
                if (value.Contains(" "))
                    val = HttpUtility.UrlEncode(value);
                else
                    val = value;

                if (Uri.IsWellFormedUriString(val, UriKind.Relative) || Uri.IsWellFormedUriString(val, UriKind.Absolute))
                {
                    topLevelWeb = val;
                }
                else
                {
                    //Modifed 1/3/07 By HRamos & Vorekhov
                    //Allow absolute urls to be included for aggregation
                    //try
                    //{
                    //    Uri uri = new Uri(value);
                    //    topLevelWeb = uri.AbsolutePath;
                    //}
                    //catch (Exception)
                    //{
                    LogError("TopLevelSite setting: Only relative URIs can be accepted: " + value);
                    throw new ArgumentException("Only relative URIs can be accepted: " + value);
                    //}
                }
            }

        }

    }
}
